{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([6.320e-03, 1.800e+01, 2.310e+00, ..., 3.969e+02, 7.880e+00,\n",
       "       1.190e+01])"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "import json\n",
    "\n",
    "datafile = '../../data/housing.data'\n",
    "data = np.fromfile(datafile,sep=' ')\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(14,)\n",
      "[6.320e-03 1.800e+01 2.310e+00 0.000e+00 5.380e-01 6.575e+00 6.520e+01\n",
      " 4.090e+00 1.000e+00 2.960e+02 1.530e+01 3.969e+02 4.980e+00 2.400e+01]\n",
      "[[6.3200e-03 1.8000e+01 2.3100e+00 ... 3.9690e+02 4.9800e+00 2.4000e+01]\n",
      " [2.7310e-02 0.0000e+00 7.0700e+00 ... 3.9690e+02 9.1400e+00 2.1600e+01]\n",
      " [2.7290e-02 0.0000e+00 7.0700e+00 ... 3.9283e+02 4.0300e+00 3.4700e+01]\n",
      " ...\n",
      " [6.0760e-02 0.0000e+00 1.1930e+01 ... 3.9690e+02 5.6400e+00 2.3900e+01]\n",
      " [1.0959e-01 0.0000e+00 1.1930e+01 ... 3.9345e+02 6.4800e+00 2.2000e+01]\n",
      " [4.7410e-02 0.0000e+00 1.1930e+01 ... 3.9690e+02 7.8800e+00 1.1900e+01]]\n"
     ]
    }
   ],
   "source": [
    "feature_names = [ 'CRIM', 'ZN', 'INDUS', 'CHAS', 'NOX', 'RM', 'AGE','DIS','RAD', 'TAX', 'PTRATIO', 'B', 'LSTAT', 'MEDV' ]\n",
    "feature_num = len(feature_names)\n",
    "data = data.reshape([data.shape[0] // feature_num,feature_num])\n",
    "x=data[0]\n",
    "print(x.shape)\n",
    "print(x)\n",
    "print(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(404, 14)"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ratio = 0.8\n",
    "offset = int(data.shape[0]*ratio)\n",
    "training_data = data[:offset]\n",
    "training_data.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [],
   "source": [
    "maximums, minimums = training_data.max(axis=0),training_data.min(axis=0)\n",
    "\n",
    "for i in range(feature_num):\n",
    "  data[:, i] = (data[:, i] - minimums[i]) / (maximums[i] - minimums[i])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "iter 9, loss 3.0135619693851954\n",
      "iter 19, loss 1.3905303520915173\n",
      "iter 29, loss 0.9789398510731633\n",
      "iter 39, loss 0.8519104371951058\n",
      "iter 49, loss 0.796679110334434\n",
      "iter 59, loss 0.7620087952836149\n",
      "iter 69, loss 0.7345027647971661\n",
      "iter 79, loss 0.7102049154038553\n",
      "iter 89, loss 0.6877602348847541\n",
      "iter 99, loss 0.6666292802667506\n",
      "iter 109, loss 0.6465647500866618\n",
      "iter 119, loss 0.627436127440271\n",
      "iter 129, loss 0.6091634513140177\n",
      "iter 139, loss 0.5916900515334876\n",
      "iter 149, loss 0.5749707070953195\n",
      "iter 159, loss 0.5589663330530807\n",
      "iter 169, loss 0.5436415385934276\n",
      "iter 179, loss 0.5289634741858285\n",
      "iter 189, loss 0.5149012643611758\n",
      "iter 199, loss 0.501425708910098\n",
      "iter 209, loss 0.48850910841845574\n",
      "iter 219, loss 0.47612514842397335\n",
      "iter 229, loss 0.46424881212366237\n",
      "iter 239, loss 0.45285630781579134\n",
      "iter 249, loss 0.44192500468264073\n",
      "iter 259, loss 0.4314333739138876\n",
      "iter 269, loss 0.42136093372451555\n",
      "iter 279, loss 0.41168819753487684\n",
      "iter 289, loss 0.40239662491014916\n",
      "iter 299, loss 0.39346857501042776\n",
      "iter 309, loss 0.3848872623762305\n",
      "iter 319, loss 0.37663671491084816\n",
      "iter 329, loss 0.36870173394058553\n",
      "iter 339, loss 0.36106785624565896\n",
      "iter 349, loss 0.3537213179624583\n",
      "iter 359, loss 0.346649020263996\n",
      "iter 369, loss 0.33983849673051036\n",
      "iter 379, loss 0.3332778823267769\n",
      "iter 389, loss 0.3269558839069081\n",
      "iter 399, loss 0.320861752171369\n",
      "iter 409, loss 0.31498525500467256\n",
      "iter 419, loss 0.30931665212574655\n",
      "iter 429, loss 0.3038466709863149\n",
      "iter 439, loss 0.29856648385582685\n",
      "iter 449, loss 0.2934676860344897\n",
      "iter 459, loss 0.2885422751388397\n",
      "iter 469, loss 0.28378263140702087\n",
      "iter 479, loss 0.2791814989735451\n",
      "iter 489, loss 0.27473196806577455\n",
      "iter 499, loss 0.2704274580767253\n",
      "iter 509, loss 0.2662617014710198\n",
      "iter 519, loss 0.2622287284829471\n",
      "iter 529, loss 0.2583228525676067\n",
      "iter 539, loss 0.25453865656803587\n",
      "iter 549, loss 0.2508709795630459\n",
      "iter 559, loss 0.2473149043622298\n",
      "iter 569, loss 0.24386574561625698\n",
      "iter 579, loss 0.24051903851213818\n",
      "iter 589, loss 0.23727052802463994\n",
      "iter 599, loss 0.23411615869644495\n",
      "iter 609, loss 0.23105206492100522\n",
      "iter 619, loss 0.22807456170331897\n",
      "iter 629, loss 0.2251801358750806\n",
      "iter 639, loss 0.2223654377418152\n",
      "iter 649, loss 0.21962727314070954\n",
      "iter 659, loss 0.21696259588890232\n",
      "iter 669, loss 0.21436850060299256\n",
      "iter 679, loss 0.21184221587147214\n",
      "iter 689, loss 0.20938109776269181\n",
      "iter 699, loss 0.20698262365182452\n",
      "iter 709, loss 0.2046443863511066\n",
      "iter 719, loss 0.20236408852841042\n",
      "iter 729, loss 0.20013953739994023\n",
      "iter 739, loss 0.19796863968354095\n",
      "iter 749, loss 0.19584939679977767\n",
      "iter 759, loss 0.1937799003085747\n",
      "iter 769, loss 0.1917583275698065\n",
      "iter 779, loss 0.18978293761680257\n",
      "iter 789, loss 0.1878520672322755\n",
      "iter 799, loss 0.18596412721669542\n",
      "iter 809, loss 0.18411759883962814\n",
      "iter 819, loss 0.1823110304650203\n",
      "iter 829, loss 0.1805430343418604\n",
      "iter 839, loss 0.17881228355206524\n",
      "iter 849, loss 0.17711750910784696\n",
      "iter 859, loss 0.17545749719119155\n",
      "iter 869, loss 0.17383108652845036\n",
      "iter 879, loss 0.17223716589338467\n",
      "iter 889, loss 0.17067467173233614\n",
      "iter 899, loss 0.16914258590550554\n",
      "iter 909, loss 0.16763993353862058\n",
      "iter 919, loss 0.1661657809795532\n",
      "iter 929, loss 0.16471923385471865\n",
      "iter 939, loss 0.1632994352203397\n",
      "iter 949, loss 0.16190556380390494\n",
      "iter 959, loss 0.16053683233137736\n",
      "iter 969, loss 0.15919248593593208\n",
      "iter 979, loss 0.15787180064420792\n",
      "iter 989, loss 0.15657408193625505\n",
      "iter 999, loss 0.15529866337555248\n"
     ]
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAhYAAAGdCAYAAABO2DpVAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjUuMywgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/NK7nSAAAACXBIWXMAAA9hAAAPYQGoP6dpAAApk0lEQVR4nO3deXDc9X3/8dce2tW9kiVL8iFjcxQDNsRgoMbkGlwoNbnayZSMw7imP9IQUzB0KLgZyGQYI6fJMLQ0NQnzC/CbgJ1kJhxhElKPuUJj8IE5DMHmtmIjy6dW52qPz++PPaS1LfDKu9+P9dnnY2Znd7/72d3PfkmtV9+f4+szxhgBAAAUgd92BwAAgDsIFgAAoGgIFgAAoGgIFgAAoGgIFgAAoGgIFgAAoGgIFgAAoGgIFgAAoGiCXn9hKpXSnj17VFdXJ5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c+KJ41SJn08mcp/nDRGyVS6wpUcffyY7cxR7cbIWDnJlFFS5qQIW9d/4TSCBQDg5OPzZf+/6IkbjoolG7I+LYCMHVSUeZxSMqWj22Xajv15mfcnU0qaTw5INSF7f97H9c0//vGP9cMf/lBdXV0677zzdN999+miiy4qdt8AADhpjIQs5lJ8koIXfP/iF7/QLbfcou9973t65ZVXdN555+mKK65Qd3d3KfoHAAAmkIKDxT333KPrrrtOy5Yt09lnn637779f1dXV+tnPflaK/gEAgAmkoGAxPDysrVu3atGiRSMf4Pdr0aJF2rhxY9E7BwAAJpaC5ljs379fyWRSra2tecdbW1v19ttvH/M9sVhMsdjINQ6i0eg4ugkAACaCkm+q39HRoUgkkru1t7eX+isBAIAlBQWL5uZmBQIB7d27N+/43r171dbWdsz3rFy5Uj09PblbZ2fn+HsLAABOagUFi1AopAsuuEAbNmzIHUulUtqwYYMWLFhwzPeEw2HV19fn3QAAgJsK3sfilltu0dKlSzV//nxddNFFuvfee9Xf369ly5aVon8AAGACKThY/P3f/7327dunO++8U11dXfrMZz6jp59++qgJnQAAoPz4jDGfsvt5cUWjUUUiEfX09DAsAgDABHG8f79LvioEAACUD4IFAAAoGoIFAAAoGs+vq5qd0sEOnAAATBzZv9ufNjXT82DR29srSezACQDABNTb26tIJDLm656vCkmlUtqzZ4/q6urk8xXvmvbRaFTt7e3q7OxktUkJcZ69w7n2BufZG5xn75TqXBtj1Nvbq6lTp8rvH3smhecVC7/fr+nTp5fs89nd0xucZ+9wrr3BefYG59k7pTjXn1SpyGLyJgAAKBqCBQAAKBpngkU4HNb3vvc9hcNh211xGufZO5xrb3CevcF59o7tc+355E0AAOAuZyoWAADAPoIFAAAoGoIFAAAoGoIFAAAoGmeCxY9//GPNnDlTlZWVuvjii7Vp0ybbXZowOjo6dOGFF6qurk4tLS366le/qh07duS1GRoa0vLly9XU1KTa2lr93d/9nfbu3ZvXZteuXVq8eLGqq6vV0tKiW2+9VYlEwsufMqGsXr1aPp9PK1asyB3jPBfP7t279c1vflNNTU2qqqrS3LlztWXLltzrxhjdeeedmjJliqqqqrRo0SK98847eZ9x8OBBLVmyRPX19WpoaNA//uM/qq+vz+ufctJKJpO64447NGvWLFVVVem0007TXXfdlXctCc7z+Lzwwgv60pe+pKlTp8rn8+nxxx/Pe71Y5/X111/XZz/7WVVWVqq9vV3//u//fuKdNw5Yt26dCYVC5mc/+5l58803zXXXXWcaGhrM3r17bXdtQrjiiivMgw8+aLZv325effVV8zd/8zdmxowZpq+vL9fm29/+tmlvbzcbNmwwW7ZsMX/5l39pLrnkktzriUTCzJkzxyxatMhs27bN/Pa3vzXNzc1m5cqVNn7SSW/Tpk1m5syZ5txzzzU33XRT7jjnuTgOHjxoTjnlFPMP//AP5uWXXzbvv/+++f3vf2/efffdXJvVq1ebSCRiHn/8cfPaa6+ZL3/5y2bWrFlmcHAw1+av//qvzXnnnWdeeukl84c//MGcfvrp5hvf+IaNn3RSWrVqlWlqajJPPfWU+eCDD8yvfvUrU1tba/7jP/4j14bzPD6//e1vzXe/+13z61//2kgyjz32WN7rxTivPT09prW11SxZssRs377drF271lRVVZmf/OQnJ9R3J4LFRRddZJYvX557nkwmzdSpU01HR4fFXk1c3d3dRpJ5/vnnjTHGHD582FRUVJhf/epXuTZ/+tOfjCSzceNGY0z6/wj8fr/p6urKtVmzZo2pr683sVjM2x9wkuvt7TVnnHGGWb9+vfn85z+fCxac5+K57bbbzKWXXjrm66lUyrS1tZkf/vCHuWOHDx824XDYrF271hhjzFtvvWUkmc2bN+fa/O53vzM+n8/s3r27dJ2fQBYvXmyuvfbavGN/+7d/a5YsWWKM4TwXy5HBoljn9b//+79NY2Nj3r8dt912mznzzDNPqL8TfihkeHhYW7du1aJFi3LH/H6/Fi1apI0bN1rs2cTV09MjSZo0aZIkaevWrYrH43nnePbs2ZoxY0buHG/cuFFz585Va2trrs0VV1yhaDSqN99808Pen/yWL1+uxYsX551PifNcTE8++aTmz5+vr3/962ppadG8efP0wAMP5F7/4IMP1NXVlXeuI5G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DPPMM8+YLVu2mAULFpgFCxbkXs8ug7z88svNq6++ap5++mkzefJklkF+itGrQozhPBfLpk2bTDAYNKtWrTLvvPOOeeSRR0x1dbX5+c9/nmuzevVq09DQYJ544gnz+uuvm6985SvHXK43b9488/LLL5sXX3zRnHHGGWW/DHK0pUuXmmnTpuWWm/761782zc3N5l//9V9zbTjP49Pb22u2bdtmtm3bZiSZe+65x2zbts189NFHxpjinNfDhw+b1tZWc80115jt27ebdevWmerqapabZt13331mxowZJhQKmYsuusi89NJLtrs0YUg65u3BBx/MtRkcHDTf+c53TGNjo6murjZf+9rXzMcff5z3OR9++KG58sorTVVVlWlubjb/8i//YuLxuMe/ZmI5MlhwnovnN7/5jZkzZ44Jh8Nm9uzZ5qc//Wne66lUytxxxx2mtbXVhMNhc9lll5kdO3bktTlw4ID5xje+YWpra019fb1ZtmyZ6e3t9fJnnNSi0ai56aabzIwZM0xlZaU59dRTzXe/+9285Yuc5/F59tlnj/nv8tKlS40xxTuvr732mrn00ktNOBw206ZNM6tXrz7hvnPZdAAAUDQTfo4FAAA4eRAsAABA0RAsAABA0RAsAABA0RAsAABA0RAsAABA0RAsAABA0RAsAABA0RAsAABA0RAsAABA0RAsAABA0RAsAABA0fx/ADPXW8mOumUAAAAASUVORK5CYII=",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "from load_data import load_data\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "from mpl_toolkits.mplot3d import Axes3D\n",
    "\n",
    "class Network(object):\n",
    "    def __init__(self, num_of_weights):\n",
    "        # 随机产生w的初始值\n",
    "        # 为了保持程序每次运行结果的一致性，此处设置固定的随机数种子\n",
    "        np.random.seed(0)\n",
    "        self.w = np.random.randn(num_of_weights, 1)\n",
    "        self.b = 0.\n",
    "        \n",
    "    def forward(self, x):\n",
    "        z = np.dot(x, self.w) + self.b\n",
    "        return z\n",
    "    \n",
    "    def loss(self, z, y):\n",
    "        error = z - y\n",
    "        cost = error * error\n",
    "        cost = np.mean(cost)\n",
    "        return cost\n",
    "    \n",
    "    def gradient(self, x, y):\n",
    "        z = self.forward(x)\n",
    "        gradient_w = (z-y)*x\n",
    "        gradient_w = np.mean(gradient_w, axis=0)\n",
    "        gradient_w = gradient_w[:, np.newaxis]\n",
    "        gradient_b = (z - y)\n",
    "        gradient_b = np.mean(gradient_b)\n",
    "        \n",
    "        return gradient_w, gradient_b\n",
    "    \n",
    "    def update(self, gradient_w5, gradient_w9, eta=0.01):\n",
    "        self.w = self.w - eta * gradient_w5\n",
    "        self.w = self.w - eta * gradient_w9\n",
    "        \n",
    "    def train(self, x, y, iterations=100, eta=0.01):\n",
    "        losses = []\n",
    "        for i in range(iterations):\n",
    "            z = self.forward(x)\n",
    "            L = self.loss(z, y)\n",
    "            gradient_w, gradient_b = self.gradient(x, y)\n",
    "            self.update(gradient_w, gradient_b, eta)\n",
    "            losses.append(L)\n",
    "            if (i+1) % 10 == 0:\n",
    "                print('iter {}, loss {}'.format(i, L))\n",
    "        return losses\n",
    "\n",
    "# 获取数据\n",
    "train_data, test_data = load_data()\n",
    "x = train_data[:, :-1]\n",
    "y = train_data[:, -1:]\n",
    "# 创建网络\n",
    "net = Network(13)\n",
    "num_iterations=1000\n",
    "# 启动训练\n",
    "losses = net.train(x, y, iterations=num_iterations, eta=0.01)\n",
    "\n",
    "# 画出损失函数的变化趋势\n",
    "plot_x = np.arange(num_iterations)\n",
    "plot_y = np.array(losses)\n",
    "plt.plot(plot_x, plot_y)\n",
    "plt.show()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
